﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileSynchronization.Helpers
{
    public static class InputHelper
    {
        public static T GetUserSelect<T>(string tips, List<KeyValuePair<string, T>> optionals)
        {
            while (true)
            {
                Console.WriteLine(tips);

                if (optionals?.Any() != true)
                    throw new NotSupportedException($"没有找到可选项");

                Console.WriteLine("请输入编码");

                for (int i = 0; i < optionals.Count; i++)
                {
                    Console.WriteLine($"{i + 1}\t{optionals[i].Key}");
                }

                var input = Console.ReadLine();
                if (int.TryParse(input, out var inputIndex) != true)
                    continue;

                if (inputIndex <= 0 || inputIndex > optionals.Count)
                {
                    Console.WriteLine($"请输入有效的选项编码");
                    continue;
                }

                return optionals[inputIndex - 1].Value;
            }
        }
    }
}
